/*
*	test.cc
*
*	22 nov. 2.008
*
*	Autor: Fénix.
*	Última actualización: 22 nov. 2.008
*
*
* test.cc is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* test.cc is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

#define WIDTH_SCREEN  800
#define HEIGHT_SCREEN 600

#define PPS 2



class Monkey {
    
    static Uint16 MAX_SIZE_X;
    static Uint16 MAX_SIZE_Y;
    
    SDL_Rect position;
    SDL_Surface *image;
    std::string name;
    
    
public:
    Monkey(SDL_Surface *image, std::string name) {
	
	this->name  = name;
	this->image = image;
	
	this->update();
	
	std::cout << this->name << " monkey has born." << std::endl;
    }
    
    ~Monkey() {
	std::cout << this->name << " monkey has died." << std::endl;
    }
    
    void draw(SDL_Surface *target) {
	SDL_BlitSurface(image, 0, target, &position);
    }
    
    void update() {
	position.x = rand() % (Monkey::MAX_SIZE_X - (image->w));
	position.y = rand() % (Monkey::MAX_SIZE_Y - (image->h));
    }
};

Uint16 Monkey::MAX_SIZE_X = WIDTH_SCREEN;
Uint16 Monkey::MAX_SIZE_Y = HEIGHT_SCREEN;



int main (int narg, char **argv) {
    
    //Iniciamos el subsistema de vídeo de SDL.
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
	std::cout << "Ooops!... Houston, we've got a problem with SDL!" << std::endl;
    }
    
    //Usamos atexit con la función de salida de SDL.
    atexit(SDL_Quit);
    
    //Creamos una superficie para ver algo. :)
    SDL_Surface *screen = SDL_SetVideoMode(WIDTH_SCREEN, HEIGHT_SCREEN, 0, SDL_ANYFORMAT | SDL_DOUBLEBUF | SDL_SWSURFACE);
    
    std::string windowName = "Test";
    windowName += " :)";
    
    SDL_WM_SetCaption(windowName.c_str(), 0);
    
    
    //Cargamos la imagen que queremos mostrar.
    SDL_Surface *monkeyTeamLogo = SDL_DisplayFormat(IMG_Load("resource/monkey2.png"));
    
    
    if (monkeyTeamLogo == 0) {
	std::cout << "Ooops!: I couldn't find your image. :(" << std::endl;
	return -1;
    }

    
    srand(time(0));
    
    std::vector<Monkey *> monkeys;
    
    monkeys.push_back(new Monkey(monkeyTeamLogo, "Juan"));
    monkeys.push_back(new Monkey(monkeyTeamLogo, "María"));

    SDL_Event event;
    
    bool exit = false;
    
    
    Uint32 t0=SDL_GetTicks();
    Uint32 t1=SDL_GetTicks();
    
    
    //Bucle principal.
    while (!exit) {
	//Actualizamos la pantalla PPS veces por segundo... o el máximo que de nuestro
	//equipo... :)
	if (t1 - t0 >= (1000/PPS)) {
	    //Recorremos el vector, usando un iterador, para actualizar los elementos.
	    std::vector<Monkey *>::iterator it;

	    for(it = monkeys.begin(); it < monkeys.end(); it++) {
		(*it)->update();
		(*it)->draw(screen);
	    }

	    SDL_Flip(screen);	

	    SDL_FillRect(screen, 0, 0);
	    
	    t0 = t1;
	} else {
	    t1 = SDL_GetTicks();
	}
	
	
	if(SDL_PollEvent(&event)) {
	     switch(event.type) {
		 case SDL_QUIT:
		     exit = true;
		     break;
		 case SDL_KEYDOWN:
		     switch (event.key.keysym.sym) {
			 case SDLK_ESCAPE:
			     exit = true;
			     break;
		     }
		     break;
	     }
	 }
    }

    //Liberamos los recursos.
    SDL_FreeSurface(monkeyTeamLogo);
    
    std::vector<Monkey *>::iterator it;

    for(it = monkeys.begin(); it < monkeys.end(); it++) {
	delete (*it);
    }
    
    
    // ... Y voilà!
    return 0;
}